package com.uiao.controller;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author cuihao
 * <p> 此示例是基于redis官方开发的Jedis框架实现的
 *
 * @date 2018/1/18
 */
public class RedisDemo {
    private static ShardedJedisPool pool;

    public static void main(String[] args) {

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxWaitMillis(1000);
        config.setMaxIdle(50); // 最大空闲连接数
        config.setMaxTotal(100); // 最大连接数 (可以自己写个线程池)
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);

        JedisShardInfo jedisShardInfo = new JedisShardInfo("39.104.164.202", 6382);
        jedisShardInfo.setPassword("123456adF");
        List<JedisShardInfo> list = new LinkedList<>();
        list.add(jedisShardInfo);
        pool = new ShardedJedisPool(config, list);
        ShardedJedis jedis = pool.getResource();
        System.out.println("连接成功");
        System.out.println(jedis.echo("fdsf"));

        /*String*/
        jedis.set("string1", "woshicuihao");
        System.out.println("存储的字符串为：" + jedis.get("string1"));

        /*List*/
        jedis.lpush("list2", "我是你爸爸");
        jedis.lpush("list2", "我是你爷爷");
        jedis.lpush("list2", "我是你爸爸");
        jedis.lpush("list2", "我是xxx");
        List<String> out = jedis.lrange("list2", 0, 2);
        System.out.println("存储的list为：");
        out.stream().forEach(e -> System.out.println(e));

        /*Hash*/
        Map<String, String> hashMap = new HashMap();
        hashMap.put("key1", "cpu");
        hashMap.put("key2", "硬盘");
        hashMap.put("key3", "内部");
        jedis.hmset("hash1", hashMap);
        List<String> h = jedis.hmget("hash1", "key1", "key3");
        System.out.println("存储的hash为：");
        h.forEach(e -> System.out.println(e));

        /*Set*/
        jedis.sadd("set1", "白桦林", "那些花儿", "那也从事我的梦想");
        System.out.println("存储的set为：");
        jedis.smembers("set1").forEach(e -> System.out.println(e));

        /*sort set*/
        jedis.zadd("sset1", 24, "金星");
        jedis.zadd("sset1", 5, "太白金星");
        jedis.zadd("sset1", 13, "你死定了");
        System.out.println("存储的sort set为：" + jedis.zrange("sset1", 0, 2).toString());

    }
}
